﻿using System.Collections.Generic;
using System.Threading.Tasks;
using GrapeCity.Forguncy.Commands;
using TencentCloud.Common;
using TencentCloud.Ocr.V20181119.Models;
using TencentCloudOcrPlugin.Resources;

namespace TencentCloudOcrPlugin.Ocr
{
    public class OcrBankCardAction : OcrActionBase
    {

        private static readonly Dictionary<string, string> ParamsDict = new Dictionary<string, string>();

        public OcrBankCardAction()
        {
            ParamsDict["CardNo"] = "卡号";
            ParamsDict["BankInfo"] = "银行信息";
            ParamsDict["ValidDate"] = "有效期";
            ParamsDict["CardType"] = "卡类型";
            ParamsDict["CardName"] = "卡名字";
            ParamsDict["BorderCutImage"] = "切片图片数据";
            ParamsDict["CardNoImage"] = "卡号图片数据";
        }

        public override List<string> GetParamList(TencentCloudApiPropertyBase properties)
        {
            var list = base.GetParamList(properties);
            list.AddRange(ParamsDict.Values);
            return list;
        }
        
        public override async Task<Dictionary<string,object>> RunAsync(TencentCloudApiClientConfig clientConfig,
            IServerCommandExecuteContext dataContext, TencentCloudApiPropertyBase properties)
        {
            var ocrBaseProperties = properties as OcrBaseProperties;
            var baseRequest = await BuildRequestAsync(ocrBaseProperties, dataContext);
            var request = new BankCardOCRRequest
            {
                ImageBase64 = baseRequest.ImageBase64,
                ImageUrl = baseRequest.ImageUrl
            };
            var response = await GetClient(clientConfig).BankCardOCR(request);
            
            return BuildResp(response, properties);
        }
        private Dictionary<string, object> BuildResp(AbstractModel tencentResp, TencentCloudApiPropertyBase properties) {
            var dict = new Dictionary<string, object>();
            if (!(tencentResp is BankCardOCRResponse response))
            {
                return dict;
            }
            if (properties.advancedSettings.TencentRawResp)
            {
                dict["原始数据"] = tencentResp;
            }
            var type = typeof(BankCardOCRResponse);
            foreach (var keyValuePair in ParamsDict)
            {
                dict[ParamsDict[keyValuePair.Key]]  = type.GetProperty(keyValuePair.Key)?.GetValue(response);
            }            
            return dict;
        }
    }
}